package com.bangdream.michelia.view.activity.exam;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.ExamContract;
import com.bangdream.michelia.entity.exam.ExamBean;
import com.bangdream.michelia.entity.exam.ExamQuestBean;
import com.bangdream.michelia.entity.exam.ExamResultDetailsBean;
import com.bangdream.michelia.entity.exam.ExamResultItemBean;
import com.bangdream.michelia.entity.exam.SubjectBean;
import com.bangdream.michelia.entity.exam.SubjectInfoBean;
import com.bangdream.michelia.presenter.ExamPresenter;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.FullyLinearLayoutManager;
import com.bangdream.michelia.tool.NoScrollViewPager;
import com.bangdream.michelia.view.activity.base.BaseActivity;
import com.bangdream.michelia.view.activity.base.SwipeBackActivity;
import com.bangdream.michelia.view.adapter.ExamSelectAnalysisAdapter;
import com.bangdream.michelia.view.adapter.SelectExamAdapter;
import com.bangdream.michelia.view.fragment.exam.BaseAnswerFragment;
import com.bangdream.michelia.view.fragment.exam.ExamDefaultFragment;
import com.bangdream.michelia.view.fragment.exam.FillBlankFragment;
import com.bangdream.michelia.view.fragment.exam.SelectExamFragment;
import com.bangdream.michelia.view.fragment.exam.SolveFragment;
import com.bangdream.michelia.view.fragment.exam.analysis.AnalysisDefaultFragment;
import com.bangdream.michelia.view.fragment.exam.analysis.FillBlankAnalysis;
import com.bangdream.michelia.view.fragment.exam.analysis.SelectExamAnalysis;
import com.bangdream.michelia.view.fragment.exam.analysis.SolveAnalysis;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @date on 2018/8/19 3:18
 * @email 909732184@qq.com
 * 答案解析
 */
public class AnswerAnalysisActivity extends SwipeBackActivity<ExamContract.IExamView,ExamPresenter<ExamContract.IExamView>>
        implements ViewPager.OnPageChangeListener,ExamContract.IExamView,OnMainActivityListener{

    private NoScrollViewPager viewPager;
    private FragmentPagerAdapter mAdapter;
    private List<ExamResultItemBean> data;
    private int nmItem = -1;
    private ExamBean examBean;
    private List<BaseAnswerFragment> answerFragments = new ArrayList<>();
    @Override
    public int getLayout() {
        return R.layout.activity_answer_analysis;
    }

    @Override
    public void findId() {
        viewPager = findViewById(R.id.viewPager);

    }

    @Override
    public void setListener() {
        viewPager.addOnPageChangeListener(this);

    }

    @Override
    public void logic() {
        initTitleBar(" ","答题详情解析","",this);
        if(getIntent().getExtras()!=null){
            examBean = getIntent().getExtras().getParcelable("data");
            data = getIntent().getExtras().getParcelableArrayList("ExamResultItemBean");
            nmItem = getIntent().getExtras().getInt("position");
        }
        setHideInput(true);
        viewPager.setNoScroll(false);
        if(data!=null){
            setViewData();
            setTitleText();
            return;
        }
        //获取答案集合
        if(examBean!=null&&examBean.getExamId()!=null) {
            mPresenter.getExamResultList(examBean.getExamIdFinish(), pd);
        }else {
            showText("数据有误，没有考试id");
        }
    }

    @Override
    public ExamPresenter createPresenter() {
        return new ExamPresenter();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void setExamList(boolean isSuccess, int page, List<ExamBean> data, String stDescribe) {

    }

    @Override
    public void setSubjectList(boolean isSuccess, List<SubjectBean> data, String stDescribe) {

    }

    @Override
    public void setSubjectDetails(boolean isSuccess, SubjectInfoBean data, String stDescribe) {

    }

    @Override
    public void initExamId(boolean isSuccess, String examId, String stDescribe) {

    }

    @Override
    public void actionSubmitExam(boolean isSuccess, boolean isFinish, String stDescribe) {

    }

    @Override
    public void actionSubmitQuest(boolean isSuccess, boolean isFinish, String stDescribe) {

    }

    @Override
    public void setExamResultList(boolean isSuccess, List<ExamResultItemBean> data, String stDescribe) {
        if(isSuccess){
            if(data!=null){
                this.data = data;
                setViewData();
                setTitleText();
            }
        }
    }

    private void setTitleText() {
        if(data==null||viewPager==null)
            return;
        ExamResultItemBean itemBean = data.get(viewPager.getCurrentItem());
        if(itemBean==null)
            return;
        tv_title.setText(itemBean.getQuestionTitle()+"");
    }

    private void setViewData() {
        if(data==null) {
            return;
        }
        for(int i = 0;i<data.size();i++) {
            ExamHelper.getInstance().getIsAnswerBegin().add(false);
        }
        /**
         * 初始化Adapter
         */
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()){

            @Override
            public int getCount() {
                return  data.size();
            }

            @Override
            public Fragment getItem(int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("index",position);
                bundle.putInt("totalIndex",getCount());
                bundle.putParcelable("data",data.get(position));
                BaseAnswerFragment answerFragment = null;
                ExamResultItemBean examResultItemBean = data.get(position);
                if(examResultItemBean==null||examResultItemBean.getQuestionType()==null){
                    return null;
                }
                if(examResultItemBean.getQuestionType().equals(ExamActivity.SubjectType.write.getValue())){
                    //填空题
                    answerFragment = new FillBlankAnalysis();
                }else if(examResultItemBean.getQuestionType().equals(ExamActivity.SubjectType.multiSelection.getValue())){
                    //多选题
                    answerFragment = new SelectExamAnalysis();
                    bundle.putBoolean("isSingle",false);
                }else if(examResultItemBean.getQuestionType().equals(ExamActivity.SubjectType.selection.getValue())){
                    //单选题
                    answerFragment = new SelectExamAnalysis();
                    bundle.putBoolean("isSingle",true);
                }else if(examResultItemBean.getQuestionType().equals(ExamActivity.SubjectType.shortexam.getValue())){
                    //简答题
                    answerFragment = new SolveAnalysis();
                }else if (examResultItemBean.getQuestionType().equals(ExamActivity.SubjectType.judgement.getValue())){
                    //判断题使用和单选题意义的界面
                    answerFragment = new SelectExamAnalysis();
                    bundle.putBoolean("isSingle",true);
                }else {
                    //未知题型
                    answerFragment = new AnalysisDefaultFragment();
                }
                answerFragment.setOnMainActivityListener(AnswerAnalysisActivity.this);
                answerFragment.setArguments(bundle);
                answerFragments.add(answerFragment);
                return answerFragment;
            }
        };
        //每个页面都缓存 不用每次划开重新加载 最大缓存10个页面
        viewPager.setOffscreenPageLimit(Math.min(10,data.size()));
        viewPager.setAdapter(mAdapter);
        if(nmItem!=-1){
            viewPager.setCurrentItem(nmItem);
        }
    }

    @Override
    public void setExamResultDetails(boolean isSuccess, ExamResultDetailsBean data, String stDescribe) {

    }

    @Override
    public void setExamQuestList(boolean isSuccess, List<ExamQuestBean> data, String stDescribe) {

    }

    @Override
    public void setExamQuList(boolean isSuccess, List<ExamBean> data, String stDescribe) {

    }

    @Override
    public void onPageToNext() {
        viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
        setTitleText();
    }

    @Override
    public void onPageToLast() {
        viewPager.setCurrentItem(viewPager.getCurrentItem()-1);
        setTitleText();
    }




}
